//
//  MainFirst_Magazine_Cell_WX.m
//  ZITHER
//
//  Created by weixin on 2019/3/15.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "MainFirst_Magazine_Cell_WX.h"
#import "MainFirst_MagazineVideo_VC_WX.h"
#import "Ugc_HomePage_VC_V3.h"

@implementation MainFirst_Magazine_Cell_WX

- (void)awakeFromNib {
    
    [super awakeFromNib];
    self.backgroundColor = HexColor(0xf5f5f5);
    [self configComment];
    [self setUI];
    [self setMasonry];
    
}

-(void)setModel:(CoverCreateListModelSub *)model
{
    _model = model;

//    [self.viewBorder mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.timeSubTitle.mas_bottom).offset(5);
//        make.right.equalTo(self).offset(-14);
//        make.bottom.equalTo(self).offset(-22);
//        make.left.equalTo(self).offset(14);
//    }];
    
    self.title.text = model.pgcTitle;
    if ([model.documentType isEqualToString:@"video"]) {
        [self.ImageV sd_setImageWithURL:[NSURL URLWithString:model.videoCoverImg] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
        self.play.hidden = NO;
    }else{
        [self.ImageV sd_setImage:model.coverImg];
        self.play.hidden = YES;
    }
    
    if (self.model.videoUrl.length>0) {
        self.ImageV.userInteractionEnabled = YES;
    }else{
        self.ImageV.userInteractionEnabled = NO;
    }
    
    if (model.isFirst) {
        self.iconFirst.hidden = NO;
    }else{
        self.iconFirst.hidden = YES;
    }
//    self.viewBorder.backgroundColor = [UIColor redColor];
    
    if (self.Style == 0) {
        
        [self.timeTitle mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(0);
            make.height.mas_equalTo(0);
        }];
        
        [self.timeSubTitle mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.timeTitle.mas_bottom).offset(0);
            make.height.mas_equalTo(0);
        }];
        
        [self.viewBorder mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.timeSubTitle.mas_bottom).offset(0);
            make.right.equalTo(self).offset(-14);
            make.bottom.equalTo(self).offset(-22);
            make.left.equalTo(self).offset(14);
        }];
        
    }else{
        
        [self.viewBorder mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.timeSubTitle.mas_bottom).offset(5);
            make.right.equalTo(self).offset(-14);
            make.bottom.equalTo(self).offset(-22);
            make.left.equalTo(self).offset(14);
        }];
        
        [self.timeTitle mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(10);
            make.height.mas_equalTo(18);
        }];
        
        [self.timeSubTitle mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.timeTitle.mas_bottom).offset(5);
            make.height.mas_equalTo(14);
        }];

        [self getTime:_model.releaseTime];
    }
}

-(void)setMasonry{
    
    [self.timeTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        make.left.equalTo(self).offset(20);
        make.height.mas_equalTo(18);
    }];
    
    [self.timeSubTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.timeTitle.mas_bottom).offset(5);
        make.left.equalTo(self).offset(20);
        make.height.mas_equalTo(14);
    }];
    
    [self.ImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.viewBorder.mas_top).offset(0);
        make.left.right.equalTo(self.viewBorder).offset(0);
        make.height.mas_equalTo(kScreenW/350.0*222);
    }];
    
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.ImageV.mas_bottom).offset(12);
        make.left.equalTo(self.viewBorder).offset(14);
        make.right.equalTo(self.viewBorder).offset(-14);
        make.bottom.equalTo(self).offset(-45);
    }];
    
}

-(void)setUI{

  
    [self addSubview:self.title];
    [self.viewBorder addSubview:self.self.ImageV];
    [self addSubview:self.play];
    [self addSubview:self.iconFirst];
    [self addSubview:self.timeTitle];
    [self addSubview:self.timeSubTitle];

    [self.play mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.ImageV.mas_left).offset(10);
        make.bottom.equalTo(self.ImageV.mas_bottom).offset(-10);
        make.height.width.mas_equalTo(40);
    }];

//    [self.ImageV mas_updateConstraints:^(MASConstraintMaker *make) {
////        make.top.equalTo(self.timeTitle.mas_bottom).offset(0);
//        make.top.equalTo(self).offset(0);
//    }];
    
    [self.iconFirst mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-24);
        make.top.equalTo(self).offset(10);
//        make.top.equalTo(self.timeTitle.mas_bottom).offset(10);
        make.width.mas_equalTo(22);
        make.height.mas_equalTo(24);
        
    }];
}

-(void)configComment{
    [self setBorderWithHiddem:YES Color:[UIColor whiteColor] width:0.5 Radius:15];
    self.viewBorder.clipsToBounds = YES;
    self.viewBorder.hidden = NO;
    self.viewBorder.backgroundColor = [UIColor whiteColor];

}


- (BOOL)checkTheDate:(NSString *)string{
    NSDateFormatter *format = [[NSDateFormatter alloc]init];
    [format setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [format dateFromString:string];
    BOOL isToday = [[NSCalendar currentCalendar] isDateInToday:date];
    
    if(isToday) {
        return YES;
    }else{
        return NO;
    }
}

-(void)getTime:(NSString *)time{


    BOOL type = [self checkTheDate:[time substringWithRange: NSMakeRange(0,10)]];
    
    //把字符串转为NSdate
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *timeDate = [dateFormatter dateFromString:time];
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:timeDate];
    long temp = 0;
    
    temp = timeInterval/3600/24;
    
    if(type){
        self.timeTitle.text = @"今日发布";
        self.timeSubTitle.hidden = YES;
        [self.timeSubTitle mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.timeTitle.mas_bottom).offset(0);
            make.height.mas_equalTo(0);
        }];
    }else{
        
        self.timeTitle.text =  [NSString stringWithFormat:@"%@日",[_model.releaseTime substringWithRange: NSMakeRange(8,2)]] ;
        self.timeSubTitle.text = [NSString stringWithFormat:@"%@年%@月",[_model.releaseTime substringWithRange: NSMakeRange(0,4)],[_model.releaseTime substringWithRange: NSMakeRange(5,2)]];

        self.timeSubTitle.hidden = NO;
        [self.timeSubTitle mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.timeTitle.mas_bottom).offset(5);
            make.height.mas_equalTo(14);
        }];
    }
        
        
}


- (BaseLab *)title{
    if (!_title) {
        _title = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 1, 1) text:@"" font:20 textColor:[UIColor blackColor] isBold:YES numberOfLines:2];
        _title.font = [UIFont fontWithName:@"PingFangSC-Medium" size:20];
        
    }
    return _title;
}

- (BaseImageV *)ImageV{
    if (!_ImageV) {
        _ImageV = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 1, 1) contentMode:2];
        /** 添加点击事件 */
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        //videoUrl
        [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            HomePageDetailModelpgcJson *model =[[HomePageDetailModelpgcJson alloc]init];
            model.src = self.model.videoUrl;
            MainFirst_MagazineVideo_VC_WX *vc = [[MainFirst_MagazineVideo_VC_WX alloc]init];
            vc.modelJson = model;
            [self.getNav pushViewController:vc animated:YES];
        }];
        _ImageV.userInteractionEnabled = YES;
        [_ImageV addGestureRecognizer:tap];
        //        [_ImageV setCornerRadiusWithRadius:15 isMasks:YES];
    }
    return _ImageV;
}

-(BaseImageV *)play{
    if (!_play) {
        _play = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 1, 1) contentMode:2];
        _play.backgroundColor = [UIColor clearColor];
        _play.image = [UIImage imageNamed:@"视频_播放"];
        
        
    }
    return _play;
}


-(BaseImageV *)iconFirst{
    if (!_iconFirst) {
        _iconFirst = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 0, 0) contentMode:UIViewContentModeCenter];
        _iconFirst.backgroundColor = [UIColor whiteColor];
        [_iconFirst setCornerRadiusWithRadius:4 isMasks:YES];
        _iconFirst.image = [self scaleToSize:[UIImage imageNamed:@"动态_置顶"] size:CGSizeMake(15, 15)];
        //        _iconFirst.image = [UIImage imageNamed:@"动态_置顶"];
        
    }
    return _iconFirst;
}

- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)newsize{
    UIGraphicsBeginImageContext(newsize);
    [img drawInRect:CGRectMake(0, 0, newsize.width, newsize.height)];
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

- (BaseLab *)timeTitle{
    if (!_timeTitle) {
        _timeTitle = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 1, 1) text:@"今日发布or日" font:20 textColor:[UIColor blackColor] isBold:YES numberOfLines:2];
        _timeTitle.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:17];
    }
    return _timeTitle;
}

- (BaseLab *)timeSubTitle{
    if (!_timeSubTitle) {
        _timeSubTitle = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 1, 1) text:@"没有or年月" font:20 textColor:[UIColor blackColor] isBold:YES numberOfLines:2];
        _timeSubTitle.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
    }
    return _timeSubTitle;
}

@end
